# !/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

class SendEmail:
    def __init__(self,receiver:str,content:str):
        self.my_sender = '1343081971@qq.com'  # 发件人邮箱账号
        self.my_pass = "flnpedicdfclhjfe"  # 发件人邮箱密码
        self.my_user = receiver  # 收件人邮箱账号，我这边发送给自己

        # 第三方 SMTP 服务
        self.msg = MIMEText(content, 'plain', 'utf-8')
        self.msg['From'] = formataddr(["张兵", self.my_sender])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
        self.msg['To'] = formataddr(["蔡松颖", self.my_user])  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
        self.msg['Subject'] = "快上清水河畔!!!"  # 邮件的主题，也可以说是标题
        pass

    def send(self):
        try:
            server = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 发件人邮箱中的SMTP服务器，端口是25
            server.login(self.my_sender, self.my_pass)  # 括号中对应的是发件人邮箱账号、邮箱密码
            server.sendmail(self.my_sender, [self.my_user, ], self.msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
            server.quit()  # 关闭连接
            print('邮件发送成功')

        except smtplib.SMTPException:
            print("Error: 无法发送邮件")
        pass

if __name__ == "__main__":
    SendEmail(receiver='zhangbingcode@163.com',content='哈哈').send()




